home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / tos.arc / minix.c < prev    next >
C/C++ Source or Header  |  1990-01-08  |  3KB  |  122 lines

  1. /*
  2.  * Load and start MINIX on the Atari ST while running TOS.
  3.  * The MINIX loadfile can be a GEMDOS file or a floppy disk,
  4.  * so that we read only blocks of 512 bytes.
  5.  * The MINIX loadfile has a format similar to MINIX-PC.
  6.  */
  7. #include "fakeunix.c"
  8.  
  9. long    size;
  10. char    *minix = "minix.img";
  11. char    blk0[512];
  12.  
  13. main(argc,argv)
  14. char **argv;
  15. {
  16.     register    fd;
  17.     register long    n, j;
  18.     register    i;
  19.     register char    *p;
  20.     register char    *bminix;
  21.     unsigned short    *wp;
  22.     int        kick();
  23.     int        (*pc)();
  24.  
  25.     if (argc > 1)
  26.         minix = argv[1];
  27.  
  28.     if ((fd = open(minix, 0)) < 0)
  29.         fatal("cannot open %s", minix);
  30.     if ((i = read(fd, blk0, (int)sizeof(blk0))) != (int)sizeof(blk0))
  31.         fatal("read block 0 failed (ask %d, ret %d)", (int)sizeof(blk0), i);
  32.     wp = (unsigned short *)&blk0[(int)sizeof(blk0)];
  33.     if (wp[-2] != 0 || wp[-3] != wp[-1])
  34.         fatal("%s: wrong format", minix);
  35.     size = wp[-4] * 512L;
  36.     size += 1000L;
  37. #ifdef __GNUC__
  38.     p = (char *)Malloc(size);    /* Malloc with long size */
  39. #else
  40.     p = (char *)gemdos(0x48,size);    /* Malloc with long size */
  41. #endif
  42.     if ((long)p <= 0)
  43.         fatal("malloc failed\n");
  44.     bminix = p;
  45.     
  46.     for (i = 0; (j = wp[-16+2*i]) != 0; i++) {
  47.         j *= 512L;
  48. #ifdef __GNUC__
  49.         n = Fread( fd, j, p);    /* Fread with long size */
  50. #else
  51.         n = gemdos(0x3f, fd, j, p);    /* Fread with long size */
  52. #endif
  53.         if (n != j)
  54.             fatal("read failed (ask %ld, ret %ld)", j, n);
  55.         p += j;
  56.         j = wp[-15+2*i] * 512L;
  57.         while (j--)
  58.             *p++ = '\0';
  59.     }
  60.     /*
  61.      * copy kick() (and some more) at the end of minix
  62.      * we have to avoid overwriting kick()
  63.      */
  64.     memcopy(p, (char *)kick, 1000);
  65.  
  66. #ifdef __GNUC__
  67.     Super(0L);        /* Super */
  68. #else
  69.     gemdos(0x20,(char *)0);        /* Super */
  70. #endif
  71.     spl7();
  72.  
  73.     pc = (int (*)())p;
  74.     (*pc)(bminix, p);
  75. }
  76.  
  77. spl7()
  78. {
  79.     int    (*pc)();
  80.     static short code[] = {
  81.         0x007C,        /*    or.w    #0x700,sr    */
  82.         0x0700,
  83.         0x4E75,        /*    rts            */
  84.     };
  85.  
  86.     pc = (int (*)())code;
  87.     (*pc)();
  88. }
  89.  
  90. memcopy(s1, s2, n) register char *s1, *s2; register n; {
  91.     while (--n >= 0)
  92.         *s1++ = *s2++;
  93. }
  94.  
  95. kick(bminix, eminix)
  96. register char *bminix;            /* begin of minix as read in */
  97. register char *eminix;            /* end   of minix as read in */
  98. {
  99.     register char    *lowmem;    /* copy it to low core */
  100.     register long    bootpc;        /* at offset 4: start address */
  101.     register int    (*pc)();    /* same but casted to func */
  102.  
  103.     bminix += 4;
  104.     bootpc = ((long *)bminix)[0];
  105.     bminix += 4;
  106.     lowmem = ((char *)8);
  107.     while ((long)lowmem < 0x0400)    /* don't clobber TOS vars */
  108.         *lowmem++ = *bminix++;
  109.     lowmem += 0x0200;        /* length of TOS area */
  110.     bminix += 0x0200;
  111.     while (bminix < eminix)
  112.         *lowmem++ = *bminix++;
  113.     pc = (int (*)())bootpc;
  114.     (*pc)();
  115. }
  116.  
  117. fatal(f, a1, a2, a3, a4, a5, a6) char *f; {
  118.     printf(f, a1, a2, a3, a4, a5, a6);
  119.     printf("\n");
  120.     exit(1);
  121. }
  122.